package com.penghaisoft.microwms.material.web.insidewarehouse.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.penghaisoft.microwms.common.base.model.BaseDictionary;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.util.ExcelExpUtil;
import com.penghaisoft.microwms.common.wms.model.RawContractChange;
import com.penghaisoft.microwms.material.web.insidewarehouse.service.IBaseDictionaryService;
import com.penghaisoft.microwms.material.web.insidewarehouse.service.IMaterialInsideService;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping(value = "/rawContractChange")
@Slf4j
public class RawContractChangeController extends BaseController {
	
		@Autowired
		private IMaterialInsideService iMaterialInsideService;
		
		@Autowired
		private IBaseDictionaryService baseDictionaryService;

		/**
		 * @Description: TODO
		 * @param @param page
		 * @param @param rows
		 * @param @param rawContractChange
		 * @param @return   
		 * @return TransResult
		 * @author lengxf
		 * @date 2018年9月14日 下午4:45:17
		 */
		@PostMapping("list")
		public TransResult list(@RequestParam(name = "page", defaultValue = "1") int page,
                            @RequestParam(name = "rows", defaultValue = "10") int rows,RawContractChange rawContractChange) {
			rawContractChange.setActiveFlag("1");
			Pager<RawContractChange> result = iMaterialInsideService.findRawContractChange(page,rows,rawContractChange);
			return success(result);
		}
		
		/** 
		* @Title: listExp 
		* @Description: 合同变更历史导出
		* @param @param rawContractChange
		* @param @param request
		* @param @param response
		* @param @return  参数说明 
		* @return String    返回类型
		* @author luoteng 
		* @date 2018年10月25日 上午10:45:02 
		* @throws 
		*/
		@GetMapping("listExp")
		public String listExp(RawContractChange rawContractChange, HttpServletRequest request, HttpServletResponse response){
			
			List<RawContractChange> dataList = iMaterialInsideService.queryRawContractChangeList(rawContractChange);
			BaseDictionary baseDictionary1 = new BaseDictionary();
			baseDictionary1.setActiveFlag("1");
			baseDictionary1.setDicType("model_type");
			List<BaseDictionary> list1 = baseDictionaryService.queryAllByCondition(baseDictionary1);
			if (dataList != null && !dataList.isEmpty()) {
				for (RawContractChange tmpData : dataList) {
					if (list1 != null && !list1.isEmpty()) {
						for (BaseDictionary tmp : list1) {
							// 判断产品类型
							if (tmp.getDicCode().equals(tmpData.getModelType())) {
								tmpData.setModelType(tmp.getDicName());
							}
						}
					}
				}
			}
			
			List<Map> columnList = new ArrayList<Map>();
			Map<String,String> columnMap = new HashMap<String,String>();
			columnMap.put("columnComment", "条码");
			columnMap.put("columnBeanName", "barcode");
			columnList.add(columnMap);
			columnMap = new HashMap<String,String>();
			columnMap.put("columnComment", "原合同号");
			columnMap.put("columnBeanName", "contractNoOld");
			columnList.add(columnMap);
			columnMap = new HashMap<String,String>();
			columnMap.put("columnComment", "新合同号");
			columnMap.put("columnBeanName", "contractNoNew");
			columnList.add(columnMap);
			columnMap = new HashMap<String,String>();
			columnMap.put("columnComment", "型号");
			columnMap.put("columnBeanName", "model");
			columnList.add(columnMap);
			columnMap = new HashMap<String,String>();
			columnMap.put("columnComment", "型号描述");
			columnMap.put("columnBeanName", "modelDesc");
			columnList.add(columnMap);
			columnMap = new HashMap<String,String>();
			columnMap.put("columnComment", "型号类型");
			columnMap.put("columnBeanName", "modelType");
			columnList.add(columnMap);
			columnMap = new HashMap<String,String>();
			columnMap.put("columnComment", "单位");
			columnMap.put("columnBeanName", "unit");
			columnList.add(columnMap);
			columnMap = new HashMap<String,String>();
			columnMap.put("columnComment", "长度 ");
			columnMap.put("columnBeanName", "amount");
			columnList.add(columnMap);
			columnMap = new HashMap<String,String>();
			columnMap.put("columnComment", "库位 ");
			columnMap.put("columnBeanName", "wlCode");
			columnList.add(columnMap);
			columnMap = new HashMap<String,String>();
			columnMap.put("columnComment", "修改人");
			columnMap.put("columnBeanName", "createBy");
			columnList.add(columnMap);
			columnMap = new HashMap<String,String>();
			columnMap.put("columnComment", "修改时间 ");
			columnMap.put("columnBeanName", "gmtCreate");
			columnList.add(columnMap);
			
			SXSSFWorkbook sw = ExcelExpUtil.generateWorkbook(dataList, columnList);
			
			OutputStream out = null;
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			String sdfDate = sdf.format(date);
			String fileName = "原材料合同变更历史查询"+sdfDate+".xlsx";// 文件名
			try {
				out = response.getOutputStream();
				response.setContentType("application/force-download");// 设置强制下载不打开
				response.setHeader("Content-Disposition","attachment;fileName="+ URLEncoder.encode(fileName, "UTF-8"));
				sw.write(out);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		
//		//记录导出日志
//		BaseUserExportLog exportLog = new BaseUserExportLog();
//		exportLog.setTableName("OdsProKcfp");
//		String expCond = JSON.toJSONString(odsProKcfp);
//		exportLog.setExpcondition(expCond);
//		if (dataList!=null) {
//			exportLog.setExportNum(dataList.size());
//		}else {
//			exportLog.setExportNum(0);
//		}
//		exportLog.setUserDefined1("excel");
//		exportLog.setUserDefined2("库存分配查询");
//		exportLog.setFactoryCode(getCurrentFactoryCode());
//		exportLog.setCreateBy(getLoginName());
//		userService.addExportLog(exportLog);
		
		return "sucess";
	}
}
